<?php 
//命名空间
namespace Home\Controller;
//导入类
use Think\Controller;

class ContainerController extends IndexController {
    //问题详情
    public function info(){
         //接收参数
        $id = I('get.id/d');
        //查找数据
        $data = M('container')->find($id);
        $link_list = M('link')->select();
        $myWhere = array('cid'=>$id);
        $relist = M('reply')->where($myWhere)->select();
        $this->assign('link_list',$link_list);
        $this->assign('list',$data);
        $this->assign('relist',$relist);
        if (empty($_SESSION['user']['name'])) {
            $this->assign('name',"<a href=".U('Index/log').">请登录</a>");
        } else {
            $this->assign('name',$_SESSION['user']['name']);
        }
        $this->display('Container/info');
    }   
    //某个人提出的问题
    public function onlyYou(){
        //查找数据
        $uid = $_SESSION['user']['id'];
        $where = array('uid'=>$uid);
        $data = M('container')->where($where)->order('id desc')->select();
        $link_list = M('link')->select();
        $myWhere = array('cid'=>$id);
        $relist = M('reply')->where($myWhere)->select();
        $this->assign('link_list',$link_list);
        $this->assign('list',$data);
        $this->assign('relist',$relist);
        if (empty($_SESSION['user']['name'])) {
            $this->assign('name',"<a href=".U('Index/log').">请登录</a>");
        } else {
            $this->assign('name',$_SESSION['user']['name']);
        }
        $this->display('Container/only');
    }  
    //删除
    public function del()
    {
        // 判断有无传参
        if (empty($_GET['id'])) {
            $this->error('没有传参');
            exit;
        }
        //接收参数
        // $id = $_GET['id'];//  不建议!!!
        $id = I('get.id/d');
        // echo $id;exit;
        if (M('container')->delete($id) > 0) {
            $this->success('恭喜您,删除成功!', U('Container/onlyYou',array('id'=>$_SESSION['user']['id'])));
        } else {
            $this->error('删除失败!');
        }
    }

    // 执行添加的操作
    public function add()
    {
        if (empty($_POST)) {
            $this->error('请填写数据');
            exit;
        }
        $myWhere = array('name'=>$_POST['tid']);
        $hehe = M('topic')->where($myWhere)->select();
        $_POST['tid'] = $hehe[0]['id'];
        //生成数据对象
        M('container')->create();
        $insertId = M('container')->add();
        //添加操作 ,返回的字自增ID
        if ($insertId > 0) {
            $this->success('添加成功',U('Container/info',array('id'=>$insertId)));
        } else {
            $this->error('添加失败');
        }
    }
}